package com.qk.management.controller;

import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.entity.dto.course.CourseDTO;
import com.qk.management.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author fantuan
 * @created 2025/10/8 21:14
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/courses")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @GetMapping
    public Result page(CourseDTO dto){
        return Result.success(courseService.page(dto));
    }

    @DeleteMapping("/{id}")
    public Result deleteCourse(@PathVariable Integer id) {
        courseService.deleteById(id);
        return Result.success();
    }

    @PostMapping
    public Result addCourse(@RequestBody Course course) {
        courseService.add(course);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getCourseById(@PathVariable Integer id) {
        return Result.success(courseService.getCourseById(id));
    }

    @PutMapping
    public Result updateCourse(@RequestBody Course course) {
        log.info("Received course: {}", course);

        courseService.update(course);
        return Result.success();
    }
    @GetMapping("/list")
    public Result list() {
        return Result.success(courseService.list());
    }

    @GetMapping("/subject/{subject}")
    public Result listBySubject(@PathVariable Integer subject) {
        return Result.success(courseService.listBySubject(subject));
    }

}
